package com.heqiang.interceptor;

import com.heqiang.constant.Constant;
import com.heqiang.entity.manager.Admin;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.util.WebUtils;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class LoginInterceptor implements HandlerInterceptor {
    private static Logger logger = LoggerFactory.getLogger(LoginInterceptor.class);
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String path = request.getContextPath();
        Admin admin = getAdmin(request);
        if (null == admin) {
            response.sendRedirect(path + "/admin/go/login");
            return false;
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
    /****************************************************************************************************************** Private *******************************************************************************************/

    /**
     * 获取session中的管理员
     *
     * @param request
     * @return com.heqiang.entity.manager.Admin
     * @Author zhangheqiang
     * @Date 2020/5/14
     **/
    public static Admin getAdmin(HttpServletRequest request) {
        Admin admin = (Admin) WebUtils.getSessionAttribute(request, Constant.SESSION_KEY);
        return admin;
    }

    /**
     * 是否远程调用
     *
     * @param request
     * @return java.lang.Boolean
     * @Author zhangheqiang
     * @Date 2020/5/14
     **/
    public static Boolean isFeign(HttpServletRequest request) {
        String authCode = getAuth(request);
        return Constant.FEIGN_AUTH.ORDER.equals(authCode);
    }

    /**
     * 获取feign调用的请求头
     *
     * @param request
     * @return java.lang.String
     * @Author zhangheqiang
     * @Date 2020/5/14
     **/
    public static String getAuth(HttpServletRequest request) {
        return request.getHeader(Constant.FEIGN_AUTH.HEADER_KEY);
    }

    /**
     * 回写数据
     *
     * @param response
     * @param status
     * @param data
     * @return void
     * @Author zhangheqiang
     * @Date 2020/5/14
     **/

    public static void sendJson(HttpServletResponse response, Integer status, String data) throws IOException {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json");
        if (null != status) {
            response.setStatus(status);
        }
        PrintWriter writer = response.getWriter();
        writer.write(data);
        writer.flush();
        writer.close();
    }

}
